package com.wanakiko.impldetails.client;

import com.alibaba.druid.pool.DruidDataSource;
import com.wanakiko.impldetails.mapper.BlogMapper;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;

/**
 * @author WanAkiko
 * @since JDK-1.8
 */
public class HowToCreateMapperStatement {

    public static void main(String[] args) {
        DataSource dataSource = getDataSource();
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("development", transactionFactory, dataSource);
        Configuration configuration = new Configuration(environment);
        configuration.addMapper(BlogMapper.class);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
        System.out.println(blogMapper.selectBlog(1));
    }

    private static DataSource getDataSource() {
        DruidDataSource duridDataSource = new DruidDataSource();
        /* jdbc:mysql://localhost:3306/exam?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC */
        duridDataSource.setUrl("jdbc:mysql://localhost:3306/exam");
        duridDataSource.setUsername("root");
        duridDataSource.setPassword("root");
        return duridDataSource;
    }

}
